﻿/// <reference path="/Scripts/libs/backbone.js" />
/// <reference path="/Scripts/libs/underscore.js" />
/// <reference path="/Scripts/libs/jquery-1.7.2.js" />


Person = Backbone.Model.extend({
    throwCount: function () {
        var myid = this.get("id");
        return Incidents.filter(
           function (inc) { return myid == inc.get("assailantId"); }).length;
    },
    hitCount: function () {
        var myid = this.get("id");
        return Incidents.filter(
            function (inc) { return myid == inc.get("victimId"); }).length;
    }
});

Incident = Backbone.Model.extend({
    AssailantName: function () {
        var assailantId = this.get("assailantId");
        var assailantPerson = People.find(function (person) { return person.get("id") == assailantId; });
        return assailantPerson.get("name");
    },
    VictimName: function () {
        var victimId = this.get("victimId");
        var victim = People.find(function (person) { return person.get("id") == victimId; });
        return victim.get("name");
    },
    url: "/api/Incident"
});

IncidentList = Backbone.Collection.extend({
    model: Incident,
    url: "/api/Incident"
});

PeopleList = Backbone.Collection.extend({
    model: Person,
    url: "/api/People"
});

var Incidents = new IncidentList();
var People = new PeopleList();

People.fetch({ success: function () {
    Incidents.fetch();
}
});

$(function () {
    PersonView = Backbone.View.extend({
        template: _.template($("#item-template").html()),
        render: function () {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    });

    PeopleView = Backbone.View.extend({
        el: $("#peopleTable"),
        initialize: function () {
            Incidents.bind("reset", this.render, this);
            Incidents.bind("change", this.render, this);
        },
        render: function () {
            this.$el.find("tr:gt(0)").remove();
            People.each(function (person) {
                var view = new PersonView({ model: person });
                $("#peopleTable").append(view.render().el);
            });
        }
    });

    var peopleView = new PeopleView();

    IncidentView = Backbone.View.extend({
        template: _.template($("#incident-template").html()),
        render: function () {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        },
        events: {
            "click .edit": "edit"
        },
        edit: function () {
            theAddIncidentView.model = this.model;
            theAddIncidentView.render();
        }
    });

    IncidentLogView = Backbone.View.extend({
        el: $("#incidentLog"),
        initialize: function () {
            Incidents.bind("reset", this.render, this);
            Incidents.bind("change", this.render, this);

        },
        render: function () {
            this.$el.empty();
            Incidents.each(function (incident) {
                var view = new IncidentView({ model: incident });
                $("#incidentLog").prepend(view.render().el);
            });

        }
    });

    var incidentLog = new IncidentLogView();

    AddIncidentView = Backbone.View.extend({
        el: $("#reportIncidents"),
        initialize: function () {
            Incidents.bind("reset", this.render, this);
        },
        render: function () {
            var personTemplate = _.template($("#person-select-template").html(), {
                model: People.toJSON()
            });
            $("#assailantSelector").html(personTemplate);
            $("#victimSelector").html(personTemplate);
            $("#assailantSelector > select").val(this.model.get("assailantId"));
            $("#victimSelector > select").val(this.model.get("victimId")),
            $("#reasonInput").val(this.model.get("reason"));

        },
        save: function () {
            this.model.set({
                assailantId: $("#assailantSelector > select").val(),
                victimId: $("#victimSelector > select").val(),
                reason: $("#reasonInput").val()
            });

            if (this.model.isNew()) {
                Incidents.create(this.model);
            }
            else {
                this.model.save();
            }

            this.model = new Incident();
            this.render();
        },
        events: {
            "click .save": "save"
        }

    });

    theAddIncidentView = new AddIncidentView({ model: new Incident() });

})